/**
 * 
 */
package edu.cmu.cs.webapp.ioms.ui.providers.order;

import java.text.DateFormat;
import java.text.NumberFormat;

import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ITableColorProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;

import edu.cmu.cs.webapp.ioms.models.OrderStatus;
import edu.cmu.cs.webapp.ioms.models.PurchaseOrder;
import edu.cmu.cs.webapp.ioms.models.impl.PurchaseOrderImpl;
import edu.cmu.cs.webapp.ioms.utils.IOMSImageHelper;
import edu.cmu.cs.webapp.ioms.utils.IOMSImages;

/**
 * @author Christian
 * 
 */
public class OrdersTableViewerLabelProvider extends ColumnLabelProvider
		implements ITableLabelProvider, ITableColorProvider {

	// table column index
	public static final int ORDER_ID = 0;
	public static final int CUSTOMER = 2;
	public static final int ORDER_STATUS = 1;
	public static final int ORDER_DATE = 3;
	public static final int SALES = 4;
	public static final int SHIPPING_DATE = 5;
	public static final int SHIPPING_ADDRESS = 6;
	public static final int SHIPPING_CITY = 7;
	public static final int SHIPPING_STATE = 8;
	public static final int SHIPPING_COUNTRY = 9;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang
	 * .Object, int)
	 */
	@Override
	public Image getColumnImage(Object element, int columnIndex) {
		if (element instanceof PurchaseOrder) {
			PurchaseOrder order = (PurchaseOrder) element;
			Image img = null;
			if (order.getStatus().equals(OrderStatus.INPROCESS)) {
				img = IOMSImageHelper.getInstance().getImage(
						IOMSImages.IMG_ORDER_INPROCESS);
			} else if (order.getStatus().equals(OrderStatus.PENDING)) {
				img = IOMSImageHelper.getInstance().getImage(
						IOMSImages.IMG_ORDER_PENDING);
			} else if (order.getStatus().equals(OrderStatus.DELIVERED)) {
				img = IOMSImageHelper.getInstance().getImage(
						IOMSImages.IMG_ORDER_DELIVERED);
			} else if (order.getStatus().equals(OrderStatus.ACCEPTED)) {
				img = IOMSImageHelper.getInstance().getImage(
						IOMSImages.IMG_ORDER_ACCEPT);
			}

			switch (columnIndex) {
			case ORDER_ID:
				return img;
			}
		}

		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang
	 * .Object, int)
	 */
	@Override
	public String getColumnText(Object element, int columnIndex) {
		if (element instanceof PurchaseOrder) {
			PurchaseOrder order = (PurchaseOrder) element;
			switch (columnIndex) {
			case ORDER_ID:
				// id
				return "#" + order.getOrder_id();
			case ORDER_STATUS:
				// order status
				return order.getStatus().getLiteral();
			case ORDER_DATE:
				// order date
				return order.getOrder_date() == null ? "" : DateFormat
						.getDateInstance().format(order.getOrder_date());
			case SHIPPING_DATE:
				// shipping date
				return order.getExpected_shipping_date() == null ? ""
						: DateFormat.getDateInstance().format(
								order.getExpected_shipping_date());
			case SHIPPING_ADDRESS:
				// shipping address
				return order.getShipping_address() == null ? "" : order
						.getShipping_address();
			case SHIPPING_CITY:
				// shipping city
				return order.getShipping_city() == null ? "" : order
						.getShipping_city();
			case SHIPPING_STATE:
				// shipping state
				return order.getShipping_state() == null ? "" : order
						.getShipping_state();
			case SHIPPING_COUNTRY:
				// shipping country
				return order.getShipping_country() == null ? "" : order
						.getShipping_country();
			case SALES:
				// sales
				return NumberFormat.getCurrencyInstance().format(
						((PurchaseOrderImpl) order).getGrossSales());
			case CUSTOMER:
				// customer
				return order.getClient().getFname() + " "
						+ order.getClient().getLname();
			default:
				return "";
			}
		}

		return "";

	}

	@Override
	public Color getBackground(Object element, int columnIndex) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Color getForeground(Object element, int columnIndex) {
		if (element instanceof PurchaseOrder) {
			if (columnIndex == SALES) {
				return Display.getDefault().getSystemColor(SWT.COLOR_BLUE);
			}
		}
		return null;

	}
}
